package com.black.play.entity;

import java.awt.*;
import java.util.Objects;
import static com.black.play.entity.Snake.NODE_SIZE;

/**
 * 食物类 不继承组件JComponent 由Snake类负责绘制便于(x,y)坐标定位
 *
 * @author ylx
 */
public class Food implements Cloneable
{
    // 横坐标
    private int x;
    // 纵坐标
    private int y;
    // 颜色
    private Color color;
    public static Color FOOD_COLOR = Color.ORANGE;
    public static Color BODY_COLOR = Color.CYAN;
    public static Color HEAD_COLOR = Color.PINK;

    public Food(int x, int y)
    {
        this.x = x;
        this.y = y;
        this.color = FOOD_COLOR;
    }

    public int getX()
    {
        return x;
    }

    public void setX(int x)
    {
        this.x = x;
    }

    public int getY()
    {
        return y;
    }

    public void setY(int y)
    {
        this.y = y;
    }

    public Color getColor()
    {
        return color;
    }

    public void setColor(Color color)
    {
        this.color = color;
    }

    public boolean touchTo(Food food){
        if (food == null) return false;
        if (this.equals(food)){
            return true;
        }
        return x>food.x- NODE_SIZE.width && x<food.x+ NODE_SIZE.width && y>food.y- NODE_SIZE.height && y<food.y+ NODE_SIZE.height;
    }

    /**
     * 重写equals  坐标一致时相同
     *
     * @param o 另一个对象
     * @return 布尔
     */
    @Override
    public boolean equals(Object o)
    {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Food food = (Food) o;
        return x == food.x && y == food.y;
    }

    @Override
    public int hashCode()
    {
        return Objects.hash(x, y);
    }

    @Override
    public String toString()
    {
        return String.format("Food{x=%d, y=%d}", x, y);
    }

    @Override
    public Food clone()
    {
        try
        {
            Food clone = (Food) super.clone();
            clone.color = FOOD_COLOR;
            return clone;
        }
        catch (CloneNotSupportedException e)
        {
            throw new AssertionError();
        }
    }
}
